package com.xianbeiying.app.ckgl.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.xianbeiying.app.ckgl.MyApplication;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.bean.DevicesBean;
import com.xianbeiying.app.ckgl.bean.LoginBean;
import com.xianbeiying.app.ckgl.bean.UrlBean;
import com.xianbeiying.app.ckgl.http.HttpCallBackUtil;
import com.xianbeiying.app.ckgl.model.GetModel;
import com.xianbeiying.app.ckgl.model.LoginModel;
import com.xianbeiying.app.ckgl.util.LogUtils;
import com.xianbeiying.app.ckgl.util.SharedPreferencesUtils;
import com.xianbeiying.app.ckgl.util.ToastUtil;

/**
 * 登录
 **/

public class LoginActivity extends BaseActivity implements View.OnClickListener, HttpCallBackUtil {
    private EditText et_username, et_userpwd;
    private Button login_btn;
    private LoginModel loginModel;
    private TextView tv_forget;
    private TextView tv_ip;
    private GetModel getModel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView() {
        MainActivity.fragmentid = 0;
        et_username = (EditText) findViewById(R.id.et_username);
        et_userpwd = (EditText) findViewById(R.id.et_userpwd);
        login_btn = (Button) findViewById(R.id.login_btn);
        tv_forget = (TextView) findViewById(R.id.tv_forget);
        tv_ip=(TextView) findViewById(R.id.tv_ip);
        loginModel = new LoginModel(this);
        loginModel.addResponseListener(this);
        getModel = new GetModel(this);
        getModel.addResponseListener(this);
        initData();
        initEnevt();
    }

    private void initData() {
        et_username.setText(SharedPreferencesUtils.getStringDate("username",""));
        et_userpwd.setText(SharedPreferencesUtils.getStringDate("userpwd",""));
    }

    private void initEnevt() {
        login_btn.setOnClickListener(this);
        tv_forget.setOnClickListener(this);
        tv_ip.setOnClickListener(this);

    }
    String username;
    String userpwd;
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_btn:
                 username = et_username.getText().toString().trim();
                 userpwd = et_userpwd.getText().toString().trim();
                if (username.isEmpty()) {
                    ToastUtil.showInfo(this, "用户名不能为空");
                    return;
                }
                if (userpwd.isEmpty()){
                    ToastUtil.showInfo(this, "密码不能为空");
                    return;
                }
//              SharedPreferencesUtils.setBooleanDate("isLogin",true);
//              startActivity(new Intent().setClass(LoginActivity.this, MainActivity.class));
//              finish();
                LogUtils.e("---",SharedPreferencesUtils.getStringDate("ip"));
                loginModel.getLoginDatas(username, userpwd, true);
                break;
            //忘记密码
            case R.id.tv_forget:
                startActivity(new Intent().setClass(LoginActivity.this, ForgotPasswordActivity.class));
                break;
            //修改IP
            case R.id.tv_ip:
                startActivity(new Intent().setClass(LoginActivity.this,ChangeIpActivity.class));
                break;
        }
    }
    private DevicesBean devicesBean;//大棚返回的数据
    private LoginBean loginBean;
    @Override
    public void onCallBackData(String url, Object content) {
        if (url.equals(UrlBean.LOGIN)) {
            //跳转到首页去
            loginBean = JSON.parseObject(new String((byte[]) content), LoginBean.class);
            if (loginBean.isR()) {
                MyApplication.loginBean=loginBean;
                //获取大棚数据
                getModel.QUWEYDEVS(true);
            } else {
                ToastUtil.showInfo(LoginActivity.this, loginBean.getERR_MSG());
            }
        }

        //大棚数据
        if (url.equals(UrlBean.QUWEYDEVS)){
            if (content != null){
                devicesBean = JSON.parseObject(new String((byte[]) content), DevicesBean.class);
                MyApplication.devicesBean = devicesBean;
                ToastUtil.showInfo(LoginActivity.this, "登录成功");
                MyApplication.isLogin = true;
                SharedPreferencesUtils.setStringDate("username", username);
                SharedPreferencesUtils.setStringDate("userpwd", userpwd);
                startActivity(new Intent().setClass(LoginActivity.this, MainActivity.class));
                finish();
            }
        }
    }
}
